﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Web.Helpers;
using System.Web.Mvc;
using System.Web.UI.WebControls;
using 个人博客.instrumentClass;
using 个人博客.SqlViews;

namespace 个人博客.Controllers
{
    public class MyHomeController : Controller
    {
        //登录页
        [HttpGet]
        public ActionResult entryPage() {
            //随机载入背景图
            ViewBag.backImage_entryPage = new backgroundImage().Image();

            ViewBag.number = "UserNumber";
            ViewBag.pwd = "UserPassword";
            if (Request.Cookies["number"] != null)
            {
                if (Request.Cookies["number"].Value != ""){ 
                    ViewBag.number = Request.Cookies["number"].Value;
                }
            }
            if (Request.Cookies["pwd"] != null) {
                if (Request.Cookies["pwd"].Value != "")
                {
                    ViewBag.pwd = Request.Cookies["pwd"].Value;
                }
            }

            return View();
        }
        //登录页判断
        [HttpPost]
        public ActionResult register(string number , string pwd)
        {
            int number1 = Convert.ToInt32(number);
            var panduan = new enter().DengLu(number1, pwd);
            if (panduan != "正确") { 
                return Json(new { State = 1, Message = panduan });
            }
            Session["number"] = number;
            Session["name"] = new enter().gainName(number1);

            Response.Cookies["number"].Value = number;
            Response.Cookies["number"].Expires = DateTime.Now.AddDays(7);
            Response.Cookies["pwd"].Value = pwd;
            Response.Cookies["pwd"].Expires = DateTime.Now.AddDays(7);
            return Json(new { State = 0, Message = "登录成功", skipPage = new sidebarSkip().Skip("主页") });
        }
        //主页(已登录)
        [HttpGet]
        public ActionResult homePage()
        {
            if (PanDuan())
            {
                Session["name"] = new enter().gainName(Convert.ToInt32(Session["number"]));
            }
            return View();
        }
        //主页(未登录)
        [HttpGet]
        public ActionResult KonghomePage() {
            //全部清除
            Session.Remove("number");
            Session.Remove("name");
            Response.Cookies.Remove("pwd");
            ViewBag.TiShi = "请登录";
            return View("homePage");
        }
        //注册页面
        [HttpGet]
        public ActionResult register() {
            //随机载入背景图
            ViewBag.backImage_register = new backgroundImage().Image();
            return View();
        }
        //注册
        [HttpPost]
        public ActionResult registerZhuChe(string Name,string Gmail,string YZM,string Pwd){
            var Verification = YZM.ToUpper();
            //验证邮箱验证码是否过期
            if (Request.Cookies["GmailVerificationCode"] == null) {
                return Json(new { State = 2, Message = "验证码已失效" });
            }
            //邮箱验证码
            if (Request.Cookies["GmailVerificationCode"].Value != Verification) {
                return Json(new { State = 2, Message = "验证码错误" });
            }
            //判断邮箱格式
            if (!new GmailClass().CheckEmail(Gmail)){
                return Json(new { State = 3, Message = "邮箱格式不正确" });
            }
            //判断邮箱是否已经使用
            if (new GmailClass().YouEmail(Gmail)){
                return Json(new { State = 3, Message = "邮箱已被使用" });
            }
            //注册
            var Number = new registerClass().Register(Name,Gmail,Pwd);
            return Json(new { State = 0, Message = "注册成功", number = Number, gmail = Gmail, skipPage = new sidebarSkip().Skip("主页") });
        }
        //发送邮件(注册)
        public ActionResult sendmail(string Gmail) {
            //验证
            if (!new GmailClass().CheckEmail(Gmail)) {
                return Json(new { State = 0, Message = "邮箱格式不正确" });
            }
            //判断邮箱是否已经使用
            if (new GmailClass().YouEmail(Gmail)){
                return Json(new { State = 2, Message = "邮箱已被使用" });
            }
            var GmailVerificationCode = new GmailClass().GmailVerificationCode();
            new GmailClass().sendGmail(Gmail, GmailVerificationCode);
            Response.Cookies["GmailVerificationCode"].Value = GmailVerificationCode;
            Response.Cookies["GmailVerificationCode"].Expires = DateTime.Now.AddMinutes(5.0);
            return Json(new { State = 1, Message = "发送成功!" });
        }
        //侧边栏跳转
        [HttpPost]
        public ActionResult sidebar(string name) {
            return Json(new { skipPage = new sidebarSkip().Skip(name) });
        }
        //页面丢失
        [HttpGet]
        public ActionResult loseEfficacy() {
            return View();
        }
        //修改密码(页面)
        [HttpGet]
        public ActionResult alterPwd() {
            if (PanDuan())
            {
                Session["name"] = new enter().gainName(Convert.ToInt32(Session["number"]));
            }

            return View();
        }
        //修改密码(数据)
        [HttpPost]
        public ActionResult alterPwd(string number,string gmail,string YZM)
        {
            var Verification = YZM.ToUpper();
            //验证邮箱验证码是否过期
            if (Request.Cookies["GmailVerificationCode"] == null)
            {
                return Json(new { State = 2, Message = "验证码已失效" });
            }
            //邮箱验证码
            if (Request.Cookies["GmailVerificationCode"].Value != Verification)
            {
                return Json(new { State = 2, Message = "验证码错误" });
            }
            //判断邮箱格式
            if (!new GmailClass().CheckEmail(gmail))
            {
                //报错
                return Json(new { State = 3, Message = "邮箱格式不正确" });
            }
            //判断账号
            int i;
            int number1 = Convert.ToInt32(number);
            if (!int.TryParse(number, out i)) {
                return Json(new { State = 4, Message = "账号格式不正确" });
            }
            if(!new alterPwd().exist(number1))
            {
                return Json(new { State = 4, Message = "账号不存在" });
            }
            if (!new alterPwd().correct(number1,gmail))
            {
                return Json(new { State = 3, Message = "邮箱不匹配" });
            }
            Session["YZM"] = Verification;
            return Json(new { State = 0, Message = "<div class='XiuGaiM'><div class='Kuang'><div class='GuanBi'><div class='GB_Xian1'></div><div class='GB_Xian2'></div></div><input type = 'text' class='MiMa1 color_Hui' /><input type = 'text' class='MiMa2 color_Hui' /><div class='Tishi'><span></span></div><div class='TiJiao_XG'>确定</div></div></div>" });
        }
        //修改密码(数据)
        [HttpPost]
        public ActionResult alterPwd1(string number, string gmail, string YZM, string pwd) {
            if (Session["YZM"].ToString() != YZM) {
                return Json(new { State = 1, Message = "出现问题,请输入原验证码" });
            }
            //判断账号
            int i;
            int number1 = Convert.ToInt32(number);
            if (!int.TryParse(number, out i))
            {
                return Json(new { State = 1, Message = "账号格式不正确" });
            }
            if (!new alterPwd().exist(number1))
            {
                return Json(new { State = 1, Message = "账号不存在" });
            }
            if (!new alterPwd().correct(number1, gmail))
            {
                return Json(new { State = 1, Message = "邮箱不匹配" });
            }
            new alterPwd().modification(number1,pwd);
            return Json(new { State = 0, Message = "修改成功" });
        }
        //发送邮件(修改)
        public ActionResult sendmailXiuGai(string Gmail)
        {
            //验证
            if (!new GmailClass().CheckEmail(Gmail))
            {
                return Json(new { State = 0, Message = "邮箱格式不正确" });
            }
            var GmailVerificationCode = new GmailClass().GmailVerificationCode();
            new GmailClass().sendGmail(Gmail, GmailVerificationCode);
            Response.Cookies["GmailVerificationCode"].Value = GmailVerificationCode;
            Response.Cookies["GmailVerificationCode"].Expires = DateTime.Now.AddMinutes(5.0);
            return Json(new { State = 1, Message = "发送成功!" });
        }


        //判断登录(判断每个页面的Cookies登录情况,然后吧 账号 存入Session["number"])
        private bool PanDuan() {
            //临时存入如果不为null,则表示是从登录页面进来的
            if (Session["number"] != null) {
                return true;
            }
            //账号为空
            if (Request.Cookies["number"] == null)
            {
                return false;
            }
            //密码为空
            if (Request.Cookies["pwd"] == null)
            {
                return false;
            }
            if (new enter().DengLu(Convert.ToInt32(Request.Cookies["number"].Value), Request.Cookies["pwd"].Value) != "正确") {
                return false;
            }
            Session["number"] = Convert.ToInt32(Request.Cookies["number"].Value);
            return true;
        }
    }
}